package com.example.sortlistview01.view;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.example.sortlistview01.util.DensityUtil;

public class SideBarView extends View {
	// 26个字母
	public static String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z", "#" };
	private Paint paint = new Paint();
	private TextView mTextDialog;
	private OnTouchSlideBarListener listener;
	private final int disCount = 2;
	private int radius;
	private int touchIndex = -1;

	public interface OnTouchSlideBarListener {
		void touchedIndex(int index);
	}

	public void setOnTouchSlideBarListener(OnTouchSlideBarListener listener) {
		this.listener = listener;
		radius = DensityUtil.dip2px(getContext(), 45);
	}

	public void setTextView(TextView mTextDialog) {
		this.mTextDialog = mTextDialog;
	}

	public SideBarView(Context context) {
		this(context, null);
	}

	public SideBarView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SideBarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	private Timer timer = new Timer();
	private MyTimerTask task = new MyTimerTask();
	
	private float x2 , x1 , x0;
	
	class MyTimerTask extends TimerTask {
		@Override
		public void run() {
			for(int i = 0; i <= radius ;i++){
				x0 = -i;
				System.out.println("$$"+ i +"$$#########MyTimerTask####");
				invalidate();
			}
		}

	}

	private void startTask() {
		timer.schedule(task, 0);
	}

	private void removeTask() {
		if (task != null) {
			task.cancel();
			task = new MyTimerTask();
		}
	}

	private void touchItem(int index) {
		touchIndex = index;
//		startTask();
		invalidate();
	}

	private void touchItem2(int index) {
		touchIndex = index;
//		removeTask();
		invalidate();
	}

	private void doRelease() {
		// setBackgroundDrawable(new ColorDrawable(0x00000000));
		mTextDialog.setVisibility(View.INVISIBLE);
		touchItem2(-1);
	}

	private void doMove(int index) {
		// setBackgroundResource(R.drawable.sidebar_background);
		mTextDialog.setVisibility(View.VISIBLE);
		mTextDialog.setText(b[index]);
		if (listener != null) {
			listener.touchedIndex(index);
		}
		touchItem(index);
	}

	@SuppressLint("NewApi")
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		int action = event.getAction();
		float y = event.getY();
		int index = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.

		if (index + 1 >= b.length) { // 解决有可能越界问题
			index = b.length - 1;
		} else if (index < 0) {
			index = 0;
		}

		switch (action) {
		case MotionEvent.ACTION_UP:
			doRelease();
			break;
		default:
			if (event.getX() >= getWidth() * 3 / 4 - radius) {
				doMove(index);
			} else {
				doRelease();
			}
			break;
		}
		return true;
	}

	private float setXpos(float xPos, int i) {
		if (touchIndex != -1) {
			if (i == touchIndex - 1) {
				xPos = xPos - 15;
			} else if (i == touchIndex) {
				xPos = xPos - 25;
			} else if (i == touchIndex + 1) {
				xPos = xPos - 15;
			}
		}
		return xPos;
	}

	/**
	 * 
	 * @param posX
	 * @param i
	 * @param type
	 *            1:sin30 , 2: sin15
	 * @return
	 */
	private float setPosX(float posX, int i, int type) {
		if (touchIndex != -1) {
			if (type == 1) { // sin30
				if (i == touchIndex - 2) {
					posX = (float) (posX - radius / 2); // radius * sin30
				} else if (i == touchIndex - 1) {
					posX = (float) (posX - (radius * Math.sqrt(3) / 2)); // radius
																			// *
																			// sin60
				} else if (i == touchIndex) {
//					posX = posX - radius; // radius
					posX = x0;
				} else if (i == touchIndex + 1) {
					posX = (float) (posX - (radius * Math.sqrt(3) / 2)); // radius
																			// *
																			// sin60
				} else if (i == touchIndex + 2) {
					posX = (float) (posX - radius / 2); // radius * sin30
				}
			} else if (type == 2) { // sin15
				if (i == touchIndex - 2) {
					posX = (float) (posX - radius * 0.256); // radius * sin15
				} else if (i == touchIndex - 1) {
					posX = (float) (posX - (radius * Math.sqrt(2) / 2)); // radius
																			// *
																			// sin45
				} else if (i == touchIndex) {
					posX = posX - radius; // radius
				} else if (i == touchIndex + 1) {
					posX = (float) (posX - (radius * Math.sqrt(2) / 2)); // radius
																			// *
																			// sin45
				} else if (i == touchIndex + 2) {
					posX = (float) (posX - radius * 0.256); // radius * sin15
				}
			}
		}
		return posX;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// setBackgroundResource(R.drawable.sidebar_background);
		paint.setColor(Color.rgb(33, 65, 98));
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		paint.setAntiAlias(true);
		paint.setTextSize(20);
		float itemWidth = getWidth();
		float itemHeight = getHeight() / b.length;
		for (int i = 0; i < b.length; i++) {
			float posX = itemWidth * 3 / 4 - paint.measureText(b[i]) / 2;
			float posY = (i + 1) * itemHeight;

			// compute posX
			posX = setPosX(posX, i, 1);
			canvas.drawText(b[i], posX, posY, paint);
		}
		super.onDraw(canvas);
	}

}
